package com.situ.mall.controller;

import com.situ.common.util.PageResult;
import com.situ.common.util.Result;
import com.situ.mall.pojo.entity.Order;
import com.situ.mall.pojo.entity.OrderItem;
import com.situ.mall.pojo.query.OrderQuery;
import com.situ.mall.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private IOrderService orderService;

    @RequestMapping("/selectByPage")
    public PageResult selectByPage(OrderQuery orderQuery) {
        PageResult pageInfo = orderService.selectByPage(orderQuery);
        System.out.println(orderQuery);
        return pageInfo;
    }
    @RequestMapping("/updateStatus")
    @ResponseBody
    public Result updateStatus(Long orderNo, Integer status) {
        Order order = new Order();
        order.setOrderNo(orderNo);
        order.setStatus(status);
        if (status == 40){
            Date date = new Date();
            order.setSendTime(date);
        }
        orderService.updateStatus(order);
        return Result.ok("修改成功！");
    }
    @RequestMapping("/updateIsDeleted")
    @ResponseBody
    public Result updateIsDeleted(Long orderNo, Byte isDeleted) {
        Order order = new Order();
        System.out.println(orderNo);
        order.setOrderNo(orderNo);
        order.setIsDeleted(isDeleted);
        Order ordersInfo = orderService.orderByOrderNo(orderNo);
        if (ordersInfo.getStatus() == 0 || ordersInfo.getStatus() == 60) {
            orderService.updateIsDeleted(order);
            return Result.ok("修改成功！");
        } else {
            return Result.error("订单尚未结束");
        }
        //return Result.ok("修改成功！");
    }
    @RequestMapping("/orderItemByOrderNo")
    @ResponseBody
    public Result orderItemByOrderNo(Long orderNo) {
        List<OrderItem> list = orderService.orderItemByOrderNo(orderNo);
        return Result.ok(list);
    }
    @RequestMapping("/deleteById")
    @ResponseBody
    public Result deleteById(Long orderNo) {
        Order ordersInfo = orderService.orderByOrderNo(orderNo);
        if (ordersInfo.getStatus() == 0 || ordersInfo.getStatus() == 60) {
            orderService.deleteById(orderNo);
            return Result.ok("删除成功！");
        } else {
            return Result.error("订单尚未结束");
        }
        //return Result.ok("修改成功！");
    }
}
